package com.lepu.app.fun.tool.szqx.view;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.app.config.UserConfig;
import com.app.utils.Const;
import com.app.utils.DateUtil;
import com.app.utils.LoginUtil;
import com.eyzhs.app.R;
import com.lepu.app.fun.tool.szqx.bean.BeanToolGrow;
import com.polonium.linechart.Line;
import com.polonium.linechart.LineChartView;
import com.polonium.linechart.LinePoint;

public class GrowCurveView extends LinearLayout {

	private Context mContext;
	private int mType;
	private LineChartView chart;
	private String[] xArray;
	private String[] yArray;

	public GrowCurveView(Context context,int type) {
		super(context);

		mContext = context;
		mType = type;
		LayoutInflater.from(mContext).inflate(R.layout.tool_grow_fragment_curve,this, true);
		init();
	}

	public GrowCurveView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		LayoutInflater.from(mContext).inflate(R.layout.tool_grow_fragment_curve,this, true);
		init();
	}

	private void init() {
		    int babySex = UserConfig.getConfigInt(mContext, Const.CONFIG_APP_BABY_SEX, 1);
		    String babyDate = UserConfig.getConfigString(mContext, Const.CONFIG_APP_BABY_DATE, "");
		    long babyMill = DateUtil.getLongTime(babyDate);
		    
		    xArray =  mContext.getResources().getStringArray(R.array.array_curve_baby_x);
		    if(mType == 1){
		    	yArray = mContext.getResources().getStringArray(R.array.array_curve_baby_weight);
		    }else if(mType == 2){
		    	yArray = mContext.getResources().getStringArray(R.array.array_curve_baby_height);
		    }else if(mType == 3){
		    	yArray = mContext.getResources().getStringArray(R.array.array_curve_baby_head);
		    }
		
		    chart = (LineChartView) findViewById(R.id.chart);
		    //加高低线
		    Line lineL = new Line(mContext);
		    Line lineH = new Line(mContext);
		    
		    
		    //男
		    if(babySex == 1){
		    	  String[] lowArray;
		    	  String[] highArray;
		    	  if(mType == 1){
		    		    lowArray = mContext.getResources().getStringArray(R.array.array_boy_weight_l);
		    		    highArray = mContext.getResources().getStringArray(R.array.array_boy_weight_h);
		    		    
		    		    for(int i=0; i<7; i++){
		    		    	float lowY = (float) ((Float.valueOf(lowArray[i]))*2.5);
		    		    	float highY = (float) ((Float.valueOf(highArray[i]))*2.5);
		    		    	lineL.addPoint(new LinePoint(mContext, i*60, lowY));
		    		    	lineH.addPoint(new LinePoint(mContext, i*60, highY));
		    		    }
				    }else if(mType == 2){
				    	lowArray = mContext.getResources().getStringArray(R.array.array_boy_height_l);
			    		highArray = mContext.getResources().getStringArray(R.array.array_boy_height_h);
			    		
		    		    for(int i=0; i<7; i++){
		    		    	float lowY = (float) ((Float.valueOf(lowArray[i]))-40);
		    		    	float highY = (float) ((Float.valueOf(highArray[i]))-40);
		    		    	lineL.addPoint(new LinePoint(mContext, i*60, lowY));
		    		    	lineH.addPoint(new LinePoint(mContext, i*60, highY));
		    		    }
				    }else if(mType == 3){
				    	lowArray = mContext.getResources().getStringArray(R.array.array_boy_head_l);
			    		highArray = mContext.getResources().getStringArray(R.array.array_boy_head_h);
			    		
			    		 for(int i=0; i<7; i++){
			    		    	float lowY = (float) ((Float.valueOf(lowArray[i])-30)*3.33);
			    		    	float highY = (float) ((Float.valueOf(highArray[i])-30)*3.33);
			    		    	lineL.addPoint(new LinePoint(mContext, i*60, lowY));
			    		    	lineH.addPoint(new LinePoint(mContext, i*60, highY));
			    		    }
				    }
		    }
		    //女
		    else if(babySex == 2){
		    	  String[] lowArray;
		    	  String[] highArray;
		    	  if(mType == 1){
		    		    lowArray = mContext.getResources().getStringArray(R.array.array_girl_weight_l);
		    		    highArray = mContext.getResources().getStringArray(R.array.array_girl_weight_h);
		    		    
		    		    for(int i=0; i<7; i++){
		    		    	float lowY = (float) ((Float.valueOf(lowArray[i]))*2.5);
		    		    	float highY = (float) ((Float.valueOf(highArray[i]))*2.5);
		    		    	lineL.addPoint(new LinePoint(mContext, i*60, lowY));
		    		    	lineH.addPoint(new LinePoint(mContext, i*60, highY));
		    		    }
				    }else if(mType == 2){
				    	lowArray = mContext.getResources().getStringArray(R.array.array_girl_height_l);
			    		highArray = mContext.getResources().getStringArray(R.array.array_girl_height_h);
			    		
		    		    for(int i=0; i<7; i++){
		    		    	float lowY = (float) ((Float.valueOf(lowArray[i]))-40);
		    		    	float highY = (float) ((Float.valueOf(highArray[i]))-40);
		    		    	lineL.addPoint(new LinePoint(mContext, i*60, lowY));
		    		    	lineH.addPoint(new LinePoint(mContext, i*60, highY));
		    		    }
				    }else if(mType == 3){
				    	lowArray = mContext.getResources().getStringArray(R.array.array_girl_head_l);
			    		highArray = mContext.getResources().getStringArray(R.array.array_girl_head_h);
			    		
			    		 for(int i=0; i<7; i++){
			    		    	float lowY = (float) ((Float.valueOf(lowArray[i])-30)*3.33);
			    		    	float highY = (float) ((Float.valueOf(highArray[i])-30)*3.33);
			    		    	lineL.addPoint(new LinePoint(mContext, i*60, lowY));
			    		    	lineH.addPoint(new LinePoint(mContext, i*60, highY));
			    		    }
				    }
		    }
		    
		    //数据线
		    Line line = new Line(mContext);
		    
		    ArrayList<BeanToolGrow> beanList;
		    for(int i=0; i<37; i++){
		    	if(i==0){
		    		 beanList = BeanToolGrow.getDataByUid(LoginUtil.getUserId());
		    	}else{
		    		 beanList = BeanToolGrow.getDataByDate(LoginUtil.getUserId(), babyMill+((i-1)*DateUtil.MONTH_MILLIS * 2), babyMill+((i)*DateUtil.MONTH_MILLIS * 2));
		    	}
		    	
		    	if(beanList.size() > 0){
	    			 BeanToolGrow item = beanList.get(0);
	    			 if(mType == 1){
	            			float x = (float) i*10;
	            			float y = (float)((Float.valueOf(item.weight))*2.5);
	            			line.addPoint(new LinePoint(mContext, x, y));
	            		}else if(mType == 2){
	            			float x = (float) i*10;
	            			float y = (float)((Float.valueOf(item.height)-40));
	            			line.addPoint(new LinePoint(mContext, x, y));
	            		}else if(mType == 3){
	            			float x = (float) i*10;
	            			float y = (float)((Float.valueOf(item.head)-30)*3.33);
	            			line.addPoint(new LinePoint(mContext, x, y));
	            		}
	    		}
		    	
		    }
		    
		    lineH.setColor(mContext.getResources().getColor(R.color.color_grow_line_high));
		    lineL.setColor(mContext.getResources().getColor(R.color.color_grow_line_low));
		    line.setColor(mContext.getResources().getColor(R.color.color_grow_line_value));
		    
	        chart.addLine(line);
	        chart.addLine(lineL);
	        chart.addLine(lineH);
            //chart.addLine(generateLine(0, 12 * 50, 10, 10, 90));

	        chart.setViewPort(0, 0, 100, 100);
	        chart.setGridSize(10, 3, 10, 3);
	        chart.setBackgroundResource(R.color.color_white);

	        Paint main = new Paint();
	        main.setColor(mContext.getResources().getColor(R.color.color_e0));  //xy线格颜色
	        main.setStrokeWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
	                                                      1,
	                                                      getResources().getDisplayMetrics()));
	        Paint sub = new Paint();
	        sub.setColor(0x00D5C670);  //xy细线格颜色
	        sub.setStrokeWidth(0);
	        chart.setHorizontalGridStyle(main, sub);
	        chart.setVerticalGridStyle(main, sub);
	        chart.setVerValuesMarginsDP(8, 30, 8, 8); //
	        chart.setHorValuesMarginsDP(0, 8, 8, 0);
	        chart.setViewPortMarginsDP(30, 30, 8, 30);

	        Paint horValPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	        horValPaint.setColor(mContext.getResources().getColor(R.color.color_bmi));  //x轴字体颜色
	        horValPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, getResources().getDisplayMetrics()));
	        Paint verValPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	        verValPaint.setColor(mContext.getResources().getColor(R.color.color_bmi));  //y轴字体颜色
	        verValPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, getResources().getDisplayMetrics()));
	        chart.setMainValuesStyle(horValPaint, verValPaint);

	        SparseArray<String> horValues = new SparseArray<String>();
	        for (int i = 0; i < xArray.length; i++) {
	            horValues.put(i * 10, xArray[i]);
	        }

	        chart.setHorValuesText(horValues);
	        
	        SparseArray<String> verValues = new SparseArray<String>();
	        for (int i = 0; i< yArray.length; i++) {
	            verValues.put(i*10, yArray[i]);
	        }
	        chart.setVerValuesText(verValues);
	        
	}

	
    public Line generateLine(int startX, int endX, int step, int minY, int maxY) {
        Line line = new Line(mContext);
//        for (int i = startX; i <= endX; i += step) {
//        	int x = i;
//        	int y = (int) (Math.random() * (maxY - minY) + minY);
//            line.addPoint(new LinePoint(mContext, x, y));
//        }
        line.addPoint(new LinePoint(mContext, 0, 20));
        line.addPoint(new LinePoint(mContext, 20, 40));
        line.addPoint(new LinePoint(mContext, 30, 50));
        line.addPoint(new LinePoint(mContext, 50, 40));
        return line;
    }

}
